package com.island.model;

import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;

@Entity
@Table(name="role")
public class Role {

	private int roleID;//角色主键ID
	private String roleName;//角色名称
	private String roleDescribe;//角色描述
	
	private Set<User> users = new LinkedHashSet<User>();//用户集合
	
	private Set<Action> actions = new LinkedHashSet<Action>();//权限集合
	
	@Id @GeneratedValue
    @Column(name="roleid", unique=true, nullable=false)
	public int getRoleID() {
		return roleID;
	}
	public void setRoleID(int roleID) {
		this.roleID = roleID;
	}
	
	@Column(name="roleName")
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	
	@Column(name="roleDescribe")
	public String getRoleDescribe() {
		return roleDescribe;
	}
	public void setRoleDescribe(String roleDescribe) {
		this.roleDescribe = roleDescribe;
	}
	
	@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE }, 
	fetch = FetchType.LAZY, mappedBy="roles") 
	public Set<User> getUsers() {
		return users;
	}
	public void setUsers(Set<User> users) {
		this.users = users;
	}
	
	@ManyToMany(fetch = FetchType.LAZY) 
	@Cascade(value={org.hibernate.annotations.CascadeType.SAVE_UPDATE})
	@JoinTable(name="roleaction", joinColumns={@JoinColumn(name="roleID")}, 
	 inverseJoinColumns={@JoinColumn(name="actionID")}) 
	public Set<Action> getActions() {
		return actions;
	}
	public void setActions(Set<Action> actions) {
		this.actions = actions;
	}
}
